Skip to content

fix(mcp): use atomic.Bool for Server.readonly to prevent data races#3759

Merged
knative-prow[bot] merged 1 commit into
knative:mainfrom
Elvand-Lie:fix/mcp-readonly-data-race
May 16, 2026
Merged

fix(mcp): use atomic.Bool for Server.readonly to prevent data races#3759
knative-prow[bot] merged 1 commit into
knative:mainfrom
Elvand-Lie:fix/mcp-readonly-data-race

Conversation

@Elvand-Lie
Copy link
Copy Markdown
Contributor

Summary

Fixes #3756

The Server.readonly field is a plain bool with no synchronization. While the current stdio transport is single-client, the MCP SDK may dispatch tool calls on separate goroutines internally. Additionally, Start() writes to readonly while handler goroutines may concurrently read it, which is a data race under the Go memory model.

Changes

  • pkg/mcp/mcp.go: Replace bool with sync/atomic.Bool. All reads use .Load(), all writes use .Store().
  • pkg/mcp/tools_deploy.go: Update readonly check to use .Load().
  • pkg/mcp/tools_delete.go: Update readonly check to use .Load().
  • Tests: Updated tools_deploy_test.go and tools_delete_test.go to use .Store().

Testing

go test ./pkg/mcp/... -count=1    # PASS

@knative-prow knative-prow Bot requested review from dsimansk and jrangelramos May 16, 2026 11:18
@knative-prow-robot knative-prow-robot added the needs-rebase Cannot be merged due to conflicts with HEAD. label May 16, 2026
@knative-prow knative-prow Bot added size/S 🤖 PR changes 10-29 lines, ignoring generated files. needs-ok-to-test 🤖 Needs an org member to approve testing labels May 16, 2026
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 16, 2026

Hi @Elvand-Lie. Thanks for your PR.

I'm waiting for a knative member to verify that this patch is reasonable to test. If it is, they should reply with /ok-to-test on its own line. Until that is done, I will not automatically test new commits in this PR, but the usual testing commands by org members will still work.

Tip

We noticed you've done this a few times! Consider joining the org to skip this step and gain /lgtm and other bot rights. We recommend asking approvers on your previous PRs to sponsor you.

Once the patch is verified, the new status will be reflected by the ok-to-test label.

I understand the commands that are listed here.

Details

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@Elvand-Lie Elvand-Lie force-pushed the fix/mcp-readonly-data-race branch from 1a97cc3 to 80c4e87 Compare May 16, 2026 11:26
@knative-prow-robot knative-prow-robot removed the needs-rebase Cannot be merged due to conflicts with HEAD. label May 16, 2026
@codecov
Copy link
Copy Markdown

codecov Bot commented May 16, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 57.10%. Comparing base (2bbf9fe) to head (c47eb3b).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main    #3759      +/-   ##
==========================================
+ Coverage   57.06%   57.10%   +0.03%     
==========================================
  Files         181      181              
  Lines       21145    21145              
==========================================
+ Hits        12067    12074       +7     
+ Misses       7855     7849       -6     
+ Partials     1223     1222       -1     
Flag Coverage Δ
e2e 35.93% <0.00%> (+0.01%) ⬆️
e2e go 31.46% <0.00%> (ø)
e2e node 27.25% <0.00%> (ø)
e2e python 31.83% <0.00%> (ø)
e2e quarkus 27.37% <0.00%> (ø)
e2e rust 26.77% <0.00%> (ø)
e2e springboot 25.27% <0.00%> (-0.02%) ⬇️
e2e typescript 27.35% <0.00%> (ø)
e2e-config-ci 28.30% <0.00%> (ø)
integration 17.35% <0.00%> (-0.03%) ⬇️
unit macos-14 45.09% <100.00%> (+0.03%) ⬆️
unit macos-latest 45.09% <100.00%> (+0.03%) ⬆️
unit ubuntu-24.04-arm 45.35% <100.00%> (+0.03%) ⬆️
unit ubuntu-latest 46.05% <100.00%> (+0.03%) ⬆️
unit windows-latest 45.14% <100.00%> (+0.03%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@Elvand-Lie Elvand-Lie force-pushed the fix/mcp-readonly-data-race branch from 80c4e87 to 09b5041 Compare May 16, 2026 11:38
@knative-prow knative-prow Bot added size/XL 🤖 PR changes 500-999 lines, ignoring generated files. and removed size/S 🤖 PR changes 10-29 lines, ignoring generated files. labels May 16, 2026
The Server.readonly field is a plain bool with no synchronization.
While the current stdio transport is single-client, the MCP SDK may
dispatch tool calls on separate goroutines internally. Additionally,
Start() writes to readonly while handler goroutines may concurrently
read it, which is a data race under the Go memory model.

Replace bool with sync/atomic.Bool for lock-free thread safety.
All reads use Load() and all writes use Store().

Also add test coverage for the readonly rejection path in both
the deploy and delete handlers.

Signed-off-by: Elvand Lie <elvandlie@gmail.com>
Signed-off-by: elvandlie@gmail.com <elvandlie@gmail.com>
@Elvand-Lie Elvand-Lie force-pushed the fix/mcp-readonly-data-race branch from 09b5041 to c47eb3b Compare May 16, 2026 11:40
@knative-prow knative-prow Bot added size/M 🤖 PR changes 30-99 lines, ignoring generated files. and removed size/XL 🤖 PR changes 500-999 lines, ignoring generated files. labels May 16, 2026
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 16, 2026

@Elvand-Lie: Cannot trigger testing until a trusted user reviews the PR and leaves an /ok-to-test message.

Details

In response to this:

/retest

Instructions for interacting with me using PR comments are available here. If you have questions or suggestions related to my behavior, please file an issue against the kubernetes-sigs/prow repository.

@lkingland
Copy link
Copy Markdown
Member

the MCP SDK may dispatch tool calls on separate goroutines internally

Thanks! This is not immediately apparent nuance. Would be nice to add this as a comment inline above the synchronized member for future reference.

@lkingland
Copy link
Copy Markdown
Member

/lgtm
/approve

@knative-prow knative-prow Bot added the lgtm 🤖 PR is ready to be merged. label May 16, 2026
@knative-prow
Copy link
Copy Markdown

knative-prow Bot commented May 16, 2026

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: Elvand-Lie, lkingland

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow knative-prow Bot added the approved 🤖 PR has been approved by an approver from all required OWNERS files. label May 16, 2026
@lkingland
Copy link
Copy Markdown
Member

/retest

@knative-prow knative-prow Bot merged commit 8a9d180 into knative:main May 16, 2026
69 of 70 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved 🤖 PR has been approved by an approver from all required OWNERS files. lgtm 🤖 PR is ready to be merged. needs-ok-to-test 🤖 Needs an org member to approve testing size/M 🤖 PR changes 30-99 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

bug(mcp): no concurrency protection on Server.readonly field

3 participants